{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Special Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import (\n",
    "    arange,\n",
    "    array,\n",
    "    diag,\n",
    "    dot,\n",
    "    eye,\n",
    "    hstack,\n",
    "    kron,\n",
    "    prod,\n",
    "    reshape,\n",
    "    tile,\n",
    "    trace,\n",
    "    vstack,\n",
    ")\n",
    "from numpy.linalg import cholesky, det, eig, eigvals, inv, lstsq, matrix_rank\n",
    "from numpy.random import randn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = arange(12.0)\n",
    "x.shape = 2, 6\n",
    "x.shape = 3, 4\n",
    "x.shape = 4, 3\n",
    "x.shape = 6, 2\n",
    "x.shape = 2, 2, 3\n",
    "x.shape = 12"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = reshape(arange(12.0), (4, 3))\n",
    "print(\"x.flatten()[1::2]:\")\n",
    "print(x.flatten()[1::2])\n",
    "print(\"x.ravel()[1::2]:\")\n",
    "print(x.ravel()[1::2])\n",
    "print(\"x.flat[1::2]:\")\n",
    "print(x.flat[1::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = randn(2, 2)\n",
    "y = randn(1, 1)\n",
    "z = randn(3, 2)\n",
    "\n",
    "pt1 = hstack((x, tile(y, (2, 3))))\n",
    "pt2 = vstack((z.T, tile(y, (1, 3))))\n",
    "pt3 = hstack((z, pt2))\n",
    "final = vstack((pt1, pt3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = reshape(arange(12.0), (2, 2, 3))\n",
    "print(\"x.squeeze():\")\n",
    "print(x.squeeze())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = array([[2, 0.5], [0.5, 4]])\n",
    "z = diag(diag(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "z = cholesky(y)\n",
    "print(\"dot(z,z.T):\")\n",
    "print(dot(z, z.T))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(\"trace(y):\")\n",
    "print(trace(y))\n",
    "D = eigvals(y)\n",
    "print(\"sum(D):\")\n",
    "print(sum(D))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(\"det(y):\")\n",
    "print(det(y))\n",
    "print(\"prod(D):\")\n",
    "print(prod(D))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(\"inv(y):\")\n",
    "print(inv(y))\n",
    "(D, V) = eig(y)\n",
    "D = 1 / D\n",
    "print(\"dot(dot(V,diag(D)),V.T):\")\n",
    "print(dot(dot(V, diag(D)), V.T))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = randn(100, 2)\n",
    "e = randn(100, 1)\n",
    "B = array([[1], [0.5]])\n",
    "y = dot(x, B) + e\n",
    "\n",
    "out = lstsq(x, y, rcond=None)\n",
    "estimate = out[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "y = array([[5, -1.5, -3.5], [-1.5, 2, -0.5], [-3.5, -0.5, 4]])\n",
    "D = eigvals(y)\n",
    "print(\"matrix_rank(y):\")\n",
    "print(matrix_rank(y))\n",
    "print(\"det(y):\")\n",
    "print(det(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = randn(100, 2)\n",
    "SigmaX = dot(x.T, x) / 100\n",
    "print(\"kron(eye(2),SigmaX):\")\n",
    "print(kron(eye(2), SigmaX))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
